import request from '@/request';
import { ApiResponse } from '@/model/common';

interface LoginParams {
  login_name: string;
  password: string;
}

interface RegisterParams {
  login_name: string;
  full_name: string;
  password: string;
  department?: string;
}

interface UserInfo {
  id: number;
  login_name: string;
  full_name: string;
  role_id: number;
  role_name: string;
  department?: string;
}

interface TokenResponse {
  token: string;
  user: UserInfo;
}

interface Permission {
  id: number;
  name: string;
  url: string;
  method: string;
  parent_id: number | null;
  level: number;
  order: number;
  icon: string | null;
  is_menu: number;
}

/**
 * 用户登录
 * @param data 登录参数
 * @returns Promise
 */
export function login(data: LoginParams): Promise<ApiResponse<TokenResponse>> {
  return request({
    url: '/api/v1/auth/login',
    method: 'post',
    data
  });
}

/**
 * 用户注册
 * @param data 注册参数
 * @returns Promise
 */
export function register(data: RegisterParams): Promise<ApiResponse<UserInfo>> {
  return request({
    url: '/api/v1/auth/register',
    method: 'post',
    data
  });
}

/**
 * 获取用户菜单权限
 * @returns Promise
 */
export function getMenuPermissions(): Promise<ApiResponse<Permission[]>> {
  return request({
    url: '/api/v1/auth/menu-permissions',
    method: 'get'
  });
}

/**
 * 退出登录
 */
export function logout(): void {
  // 清除本地存储的token和用户信息
  localStorage.removeItem('token');
  localStorage.removeItem('userInfo');
}
